package com.example.newinfinity.View.PencilPicker

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import com.example.newinfinity.R

/*调节画笔粗细和填充状态的视图弹窗内的seekbar*/
class SeekBarView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    //获取自定义属性的值
    private var attrsArray = context.obtainStyledAttributes(attrs, R.styleable.SeekBarView)
    private var mMin = attrsArray.getInteger(R.styleable.SeekBarView_mMin, 1)
    private var mMax = attrsArray.getInteger(R.styleable.SeekBarView_mMax, 50)
    private var mProgress = attrsArray.getInteger(R.styleable.SeekBarView_mProgress, 25)
        set(value) {
            field = value
            progressCallback(field)
        }

    //实时将进度值callback出去（PencilPickerView）,显示文本值
    var progressCallback: (Int) -> Unit = {}

    //绘制seekbar以及滑动小圆点的画笔
    private var mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        this.style = Paint.Style.FILL
        this.color = Color.parseColor("#EEEEEE")
        this.strokeWidth = 3f
    }

    //seekbar中的实时进度条rectF区域
    private var seekBarChooseRectF = RectF()

    //seekbar中的完整进度条rectF区域
    private var seekBarAllRectF = RectF()

    //绘制seekbar的rectF区域的圆角半径
    private var seekBarRX = 0f
    private var seekBarRY = 0f

    //seekbar滑动时小圆点Thumb的坐标信息
    private var dotR = 24f   //半径
    private var dotRX = 0f  //中心点x坐标
    private var dotRY = 0f  //中心点y坐标


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //确定绘制seekbar中rectF区域的圆角半径
        seekBarRX = measuredWidth / 2f
        seekBarRY = measuredWidth / 2f

        //seekbar滑动时小圆点Thumb的坐标信息
        dotRX = measuredWidth / 2f
        dotRY = (mProgress.toFloat() / mMax) * measuredHeight

        seekBarAllRectF.apply {
            this.left = measuredWidth / 2 - 15f
            this.right = measuredWidth / 2 + 15f
            this.top = 0f
            this.bottom = measuredHeight.toFloat()
        }

        seekBarChooseRectF.apply {
            this.left = measuredWidth / 2 - 15f
            this.right = measuredWidth / 2 + 15f
            this.top = 0f
            //两个整数相除的时候会自动取整 4/10 = 0
            this.bottom = (mProgress.toFloat() / mMax) * measuredHeight
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                val temp = ((event.y / measuredHeight) * mMax).toInt()
                if (temp in 1..100) {
                    mProgress = ((event.y / measuredHeight) * mMax).toInt()
                    //同步进度条和小圆点的位置
                    dotRY = (mProgress.toFloat() / mMax) * measuredHeight
                    seekBarChooseRectF.bottom = (mProgress.toFloat() / mMax) * measuredHeight
                    //重新绘制
                    invalidate()
                }
            }

            MotionEvent.ACTION_UP -> {}
        }
        return true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //绘制背景底色RectF
        mPaint.color = Color.parseColor("#EEEEEE")
        canvas.drawRoundRect(seekBarAllRectF, seekBarRX, seekBarRY, mPaint)
        //绘制进度底色RectF
        mPaint.color = Color.parseColor("#64B5F6")
        canvas.drawRoundRect(seekBarChooseRectF, seekBarRX, seekBarRY, mPaint)
        //绘制滑动小圆点
        mPaint.color = Color.parseColor("#2196F3")
        canvas.drawCircle(dotRX, dotRY, dotR, mPaint)
    }
}